<template>
  <div id="larry_layout" class="body-container">
    <div class="login-container">
      <div class="image"></div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="layui-form"
      >
        <div class="title-container"><p class="name">{{ title }}</p></div>
        <div class="input-container">
          <el-form-item prop="username" class="username-container">
            <img src="../assets/images/login-zh.png" alt="">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="password" class="username-container">
            <img src="../assets/images/login-mm.png" alt="">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaOnOff">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon
                slot="prefix"
                icon-class="validCode"
                class="el-input__icon input-icon"
              />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-checkbox
            v-model="loginForm.rememberMe"
            style="margin: 0px 0px 25px 0px"
          >记住密码</el-checkbox
          >
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              type="primary"
              class="login-button"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </div>
        <p class="text">技术支持: 深圳市行知网络科技有限公司</p>
      </el-form>
    </div>
  </div>

</template>

<script>
import {getCodeImg} from '@/api/login'
import Cookies from 'js-cookie'
import {encrypt, decrypt} from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      nowTime: new Date().getFullYear(),
      codeUrl: '',
      cookiePassword: '',
      title: process.env.VUE_APP_TITLE,
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: '',
        tenantCode: 'hncx',
      },
      loginRules: {
        username: [
          {required: true, trigger: 'blur', message: '请输入您的账号'},
        ],
        password: [
          {required: true, trigger: 'blur', message: '请输入您的密码'},
        ],
        code: [{required: true, trigger: 'change', message: '请输入验证码'}]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: false,
      // 注册开关
      register: false,
      redirect: undefined,
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie()
    let that = this
    document.onkeydown = function (e) {
      var keycode = document.all ? event.keyCode : e.which
      if (keycode == 13) {
        that.handleLogin()
        return false
      }
    }
  },
  methods: {
    jump(type) {
      if (type == 1) {
        window.open('http://www.shopec.com.cn/') //  跳转链接
      }
      if (type == 2) {
        window.open('http://www.shopec.com.cn/list-1-1.html') //  跳转链接
      }
      if (type == 3) {
        window.open('http://www.shopec.com.cn/list-50-1.html') //  跳转链接
      }
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        tenantCode:'hncx',
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, {expires: 30})
            Cookies.set('password', encrypt(this.loginForm.password), {
              expires: 30,
            })
            Cookies.set('rememberMe', this.loginForm.rememberMe, {
              expires: 30,
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.loginForm.tenantCode=this.loginForm.username==='admin'?'xzwl':'hncx'
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
              this.$router.push({path: this.redirect || '/'}).catch(() => {
              })
            })
            .catch(() => {
              this.loading = false
              if (this.captchaOnOff) {
                this.getCode();
              }
            })
        }
      })
    },
  },
}
</script>

<style scoped>
.body-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-end;
  width: 100%;
  height: 100%;
  background-image: url('../assets/images/login.png');
  background-size: cover;
}

.layui-form {
  width: auto;
  position: relative;
  margin: 0 auto;
}

.login-container {
  width: 70vw;
  height: 37vw;
  background-color: #fff;
  background-size: cover;
  margin: auto;
  position: relative;
  display: flex;
  box-shadow: 0px 10px 60px 0px rgba(4, 118, 135, 0.2);
  border-radius: 40px;
}

.image {
  width: 37vw;
  height: 37vw;
  background-image: url('../assets/images/login-left.png');
  background-size: cover;
}

.login-container .layui-form {
  padding-top: 9%;
}

.logo-container {
  text-align: center;
  margin: auto;
  margin-bottom: 3%;
}

.logo-container img {
  width: 156px;
  height: 45px;
}

.title-container {
  margin: auto;
  text-align: center;
}

.title-container .name {
  height: 50px;
  font-size: 30px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 600;
  color: #333333;
  line-height: 50px;
  margin-bottom: 8px;
}

img {
  width: 100%;
}

/deep/ input:focus {
  outline: none;
}

.input-container {
  height: 31%;
  margin: 11% auto 15px;
}

.username-container {
  width: 21vw;
  height: 2.5vw;
  display: flex;
  align-items: center;
  background: #F4F8F8;
  border-radius: 4px;
  margin: auto;
  margin-bottom: 24px;
  /*opacity: 0.7;*/
}
/deep/ .el-form-item__content {
  width: 21vw;
  height: 2.5vw;
  display: flex;
  align-items: center;
  background: #F4F8F8;
  border-radius: 4px;
  margin: auto;
  margin-bottom: 24px;
  /*opacity: 0.7;*/
}
/deep/ .username-container input {
  height: 30px;
  border: none;
  background-color: #f4f8f8 !important;
  padding-left: 8px;
  width: 100%;
}

/deep/ .input-container img {
  height: 20px;
  width: 20px;
  margin: 0 10px;
}

/deep/ .input-container input {
  height: 30px;
  border: none;
  background-color: #f4f8f8 !important;
  padding-left: 8px;
}

.login-button {
  width: 21vw;
  height: 3.1vw;
  margin: auto;
  text-align: center;
  align-items: center;
  display: flex;
  justify-content: center;
  font-size: 20px;
  color: #fff;
  font-family: SourceHanSansCN-Regular;
  font-weight: 400;
  cursor: pointer;
  background: #5473E8;
  border-radius: 4px;
}

.text {
  position: absolute;
  bottom: 40px;
  width: 100%;
  font-size: 14px;
  font-family: SourceHanSansCN-Regular;
  font-weight: 400;
  color: rgba(136, 136, 136, 1);
  text-align: center;
}

@media screen and (max-width: 1440px) {
  .title-container > .name {
    font-size: 28px;
  }

  .login-button {
    font-size: 18px;
  }
}
</style>
